<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="项目编码" prop="ItemCode">
        <el-input
          v-model="queryParams.ItemCode"
          clearable
          placeholder="请输入项目编码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="ItemName">
        <el-input
          v-model="queryParams.ItemName"
          clearable
          placeholder="请输入项目名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="MnemonicCode">
        <el-input
          v-model="queryParams.MnemonicCode"
          clearable
          placeholder="请输入拼音助记码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >提交申请
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ItemList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="id" prop="Item.Id"/>
      <el-table-column align="center" label="项目编码" prop="Item.ItemCode"/>
      <el-table-column align="center" label="项目名称" prop="Item.ItemName"/>
      <el-table-column align="center" label="单位" prop="Item.Unit"/>
      <el-table-column align="center" label="单价" prop="Item.Price"/>
      <el-table-column align="center" label="执行科室" prop="DeptId"/>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.size"
      :page.sync="queryParams.current"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listItem} from "@/api/system/Item";
import {addPatientCosts} from "@/api/system/PatientCosts";
import {addCheckDetailed} from "@/api/system/CheckDetailed";
import getNowFormatDate from "@/utils/getDate";

export default {
  name: "Item",
  data() {
    return {
      MedicalId: null,
      loading: true,
      rows: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      ItemList: [],
      queryParams: {
        current: 1,
        size: 10,
        ItemCode: null,
        ItemName: null,
        DeptId: null,
        MnemonicCode: null,
      },
    };
  },
  mounted() {
    this.getList()
    this.MedicalId = this.$route.query.MedicalId;
  },
  methods: {
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.ItemList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.rows = selection;
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.$confirm("确定申请上述项目的检查吗？").then(() => {
        this.rows.forEach((row) => {
          let checkDetailed = {
            Id: null,
            MedicalId: this.MedicalId,
            CheckProjId: row.Item.Id,
            DeptId: row.Item.DeptId,
            FromUserId: localStorage.getItem("userId"),
            CreationTime: getNowFormatDate(),
            Position: "疾病检查",
            CheckState: 1,
            Price: row.Item.Price,
            InspectTime: null,
            Result: null,
            ResultTime: null,
            EnterUserId: null,
            CheckUserId: null,
            DelMark: null
          };
          addCheckDetailed(checkDetailed).then(() => {
            addPatientCosts({
              Id: null,
              RegisterId: this.$route.query.RegisterId,
              ItemId: 2,
              Price: checkDetailed.Price,
              State: 0,
              DelMark: null
            })
          })
        })
        this.$alert("添加成功！").then(() => {
          this.$router.back()
        })
      })
    }
  }
};
</script>
